<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:14 $)
// $RCSfile: admin_departments.php,v $ : $Revision: 1.16 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_departments.php");
require_once ("./includes/functions_html.php");
require_once ("./includes/functions_template.php");
require_once ("./includes/functions_users.php");

$departments = new Departments;

$template->loadLanguageSection("departments");


/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing department data
*/
function _departmentFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$modtitle = "module_". $record["departmentmodule"];
	$record["title"] = '<a href="index.php?_m=core&_a=editdepartment&departmentid='.$record["departmentid"].'">'.htmlspecialchars($record["title"]).'</a>';
	$record["departmentmodule"] = $_SWIFT["language"][$modtitle];
	$record["departmenttype"] = iif($record["departmenttype"]=="public", $_SWIFT["language"]["public"], $_SWIFT["language"]["private"]);
	$record["options"] = '<a href="index.php?_m=core&_a=editdepartment&departmentid='. $record["departmentid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=core&_a=managedepartments&do=delete&departmentid='. $record["departmentid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';
	return $record;
}

/**
* Mass Action: Delete Departments
*/
function _maDeleteDepartments()
{
	global $_SWIFT, $departments;

	if (!is_array($_POST["itemid"]) || $_SWIFT["isdemo"] == true)
	{
		return false;
	}

	$departments->deleteDepartments($_POST["itemid"]);
}


/**
* ###############################################
* Insert a New Department
* ###############################################
*/
if ($eventaction == "insertdepartment")
{
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			// Insert the department

			$_POST["displayorder"] = iif(empty($_POST["displayorder"]), "1", $_POST["displayorder"]);

			$depid = $departments->insertDepartment($_POST["title"], $_POST["departmentmodule"], $_POST["tgroupid"], $_POST["type"], $_POST["displayorder"], $_POST["isdefault"]);

			printRedirect($_SWIFT["language"]["redirect_depinsert"], "index.php?_m=core&_a=managedepartments&insertdepid=".$depid);
			exit;
		}
	}

	$interface->adminHeader($_SWIFT["language"]["departments"]." > ".$_SWIFT["language"]["insert"], 3);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=managedepartments" title="'.$_SWIFT["language"]["departments"].'">'.$_SWIFT["language"]["departments"].'</a> &raquo; <a href="index.php?_m=core&_a=insertdepartment" title="'.$_SWIFT["language"]["insert"].'">'.$_SWIFT["language"]["insert"].'</a>', $_SWIFT["language"]["desc_departments"] , -1);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderDepartmentForm(INSERT);

	$template->assign("backurl", "index.php?_m=core&_a=managedepartments");

	$interface->adminFooter();


/**
* ###############################################
* MANAGE DEPARTMENTS
* ###############################################
*/
} else if ($eventaction == "managedepartments") {
	if ($_GET["do"] == "delete" && $_GET["departmentid"] != "")
	{
		if (isset($_SWIFT["departmentcache"][$_GET["departmentid"]]) && $_SWIFT["isdemo"] != true)
		{
			$infomessage = sprintf($_SWIFT["language"]["depdeleteconfirmation"], $_SWIFT["departmentcache"][$_GET["departmentid"]]["title"]);
			$departments->deleteDepartments(array($_GET["departmentid"]));
		}
	}

	if (trim($_REQUEST["insertdepid"]) != "")
	{
		$infomessage = sprintf($_SWIFT["language"]["depinsertconfirmation"], $_SWIFT["departmentcache"][$_REQUEST["insertdepid"]]["title"]);
	} else if (!empty($_GET["updatedepid"])) {
		$infomessage = sprintf($_SWIFT["language"]["depupdateconfirm"], $_SWIFT["departmentcache"][$_GET["updatedepid"]]["title"]);		
	}
	

	$options["recordsperpage"] = "6";
	$options["sortby"] = "title";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteDepartments";

	$options["idname"] = "departmentid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["deptitle"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'departments` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;';
	$options["advancedsearch"][1]["title"] = $_SWIFT["language"]["deptype"];
	$options["advancedsearch"][1]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'departments` WHERE `departmenttype` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_departmentFields";

	$fields[0]["name"] = "title";
	$fields[0]["title"] = $_SWIFT["language"]["deptitle"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "departmenttype";
	$fields[1]["title"] = $_SWIFT["language"]["deptype"];
	$fields[1]["width"] = "120";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "departmentmodule";
	$fields[2]["title"] = $_SWIFT["language"]["depmodule"];
	$fields[2]["width"] = "150";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "150";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["departments"]." > ".$_SWIFT["language"]["manage"], 3);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=managedepartments" title="'.$_SWIFT["language"]["departments"].'">'.$_SWIFT["language"]["departments"].'</a>', $_SWIFT["language"]["desc_departments"] , -1);
	printInfoBox($infomessage);

	$grid->start("departments", $_SWIFT["language"]["departmentlist"], 'SELECT * FROM `'. TABLE_PREFIX .'departments` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'departments`;', 'SELECT * FROM `'. TABLE_PREFIX .'departments` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	
	$grid->display("departments");
	$interface->adminFooter();





/**
* ###############################################
* EDIT DEPARTMENT
* ###############################################
*/
} else if ($eventaction == "editdepartment") {
	// Get the department data
	$_department = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."departments` WHERE `departmentid` = '". intval($_REQUEST["departmentid"]) ."';");
	if (empty($_department["departmentid"]))
	{
		trigger_error($_SWIFT["language"]["invaliddepartment"], E_USER_ERROR);
		exit;
	}
	

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			// Update the department

			$_POST["displayorder"] = iif(empty($_POST["displayorder"]), "1", $_POST["displayorder"]);

			updateDepartment($_department["departmentid"], $_POST["title"], $_POST["departmentmodule"], $_POST["tgroupid"], $_POST["type"], $_POST["displayorder"], $_POST["isdefault"]);

			printRedirect(sprintf($_SWIFT["language"]["depupdateconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=core&_a=managedepartments&updatedepid=".$_department["departmentid"]);
			exit;
		}
	}


	// Ok now merge the variables
	$_POST = array_merge($_POST, $_department);

	$interface->adminHeader($_SWIFT["language"]["departments"]." > ".$_SWIFT["language"]["edit"], 3);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=managedepartments" title="'.$_SWIFT["language"]["departments"].'">'.$_SWIFT["language"]["departments"].'</a> &raquo; <a href="index.php?_m=core&_a=editdepartment&departmentid='. intval($_department["departmentid"]) .'" title="'.htmlspecialchars($_department["title"]).'">'.htmlspecialchars($_department["title"]).'</a>', $_SWIFT["language"]["desc_departments"] , -1);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderDepartmentForm(EDIT);

	$template->assign("backurl", "index.php?_m=core&_a=managedepartments");

	$interface->adminFooter();
}


?>